<?php

class Home extends Controller {


        /**
         * Home class constructor.
         */
	function Home()
	{
		parent::Controller();

                //Send those not logged in back to the home page
                if(!$this->session->userdata('username'))
                   redirect(base_url());
	}

        /**
         * Index page method.
         */
	function index()
	{   
                $data['content'] = 'home_main_content';

                $this->load->model('UserModel');
                $userData = $this->UserModel->getUserInfo($this->session->userdata('username'));

                $data['fname'] = $userData['fname'];

                $this->load->model('StudentModel');
                $data['students_exist'] = $this->StudentModel->studentsExist($this->session->userdata('username'));


                $this->load->view('loggedin_template', $data);
	}

        /**
         * Method for adding a new student.
         */
        function newStudent()
        {
            $this->load->model('UserModel');
            $userData = $this->UserModel->getUserInfo($this->session->userdata('username'));

            $data['fname'] = $userData['fname'];
            $data['content'] = 'newstudent_form';

                    //Set attributes for the gradelevel field
                    $data['gradelevel_attributes'] = array(
                                                    'name' => 'gradelevel',
                                                    'maxlength' => '2',
                                                    'size' => '2'
                                                     );

                    //Set attributes for the school field
                    $data['school_attributes'] = array(
                                                    'name' => 'school',
                                                    'size' => '25'
                                                     );

                    //Set attributes for the month field
                    $data['month_attributes'] = array(
                                                    'name' => 'month',
                                                    'maxlength' => '2',
                                                    'size' => '2'
                                                     );

                    //Set attributes for the month field
                    $data['day_attributes'] = array(
                                                    'name' => 'day',
                                                    'maxlength' => '2',
                                                    'size' => '2'
                                                     );

                    //Set attributes for the year field
                    $data['year_attributes'] = array(
                                                    'name' => 'year',
                                                    'maxlength' => '4',
                                                    'size' => '4'
                                                     );

                //Check to see if the user is accessing the form, or submitting it
                if(!$this->input->post('postback'))
                {
                    $this->load->view('loggedin_template', $data);
                }

                else
                {
                    $this->load->model('StudentModel');

                    //Begin the form validation process
                    $this->load->library('form_validation');

                    //Set the validation rules
                    $this->form_validation->set_rules('fname', 'First Name', 'trim|required|max_length[30]');
                    $this->form_validation->set_rules('lname', 'Last Name', 'trim|required|max_length[30]');
                    $this->form_validation->set_rules('school', 'School', 'trim|required|max_length[50]');
                    $this->form_validation->set_rules('gradelevel', 'Grade Level', 'trim|required|max_length[2]');
                    $this->form_validation->set_rules('month', 'DOB Month', 'trim|required|max_length[2]|numeric');
                    $this->form_validation->set_rules('day', 'DOB Day', 'trim|required|max_length[2]|numeric');
                    $this->form_validation->set_rules('year', 'DOB Year', 'trim|required|min_length[2]|max_length[4]|numeric');

                    //Send back to the form to display errors
                    if($this->form_validation->run() == FALSE)
                    {
                        $this->load->view('loggedin_template', $data);
                    }
                    else
                    {
                        //Convert the date inputs to a MySQL style date
                        $dob = $this->input->post('year') . '-' . $this->input->post('month') . '-' . $this->input->post('day');

                        //Load POST data into an array.
                        $studentData = array();
                        $studentData['fname'] = $this->input->post('fname');
                        $studentData['lname'] = $this->input->post('lname');
                        $studentData['school'] = $this->input->post('school');
                        $studentData['dob'] = $dob;
                        $studentData['gradelevel'] = $this->input->post('gradelevel');

                        //Insert the data
                        $this->StudentModel->addNewStudent($studentData, $this->session->userdata('username'));
                       
                        //Go back to the admin page
                        $data['content'] = 'student_addsuccess';
                        $this->load->view('loggedin_template', $data);
                    }
                }
        }

        /**
         * Method for displaying the account admin page.
         */
        function accountadmin()
        {
                $data['content'] = 'home_accountadmin';

                $this->load->model('UserModel');
                $userData = $this->UserModel->getUserInfo($this->session->userdata('username'));

                $data['fname'] = $userData['fname'];

                $this->load->view('loggedin_template', $data);       
        }

        /**
         * MEthod for displaying the about us page.
         */
        function aboutus()
        {
                $this->load->model('UserModel');
                $userData = $this->UserModel->getUserInfo($this->session->userdata('username'));

                $data['fname'] = $userData['fname'];

                $data['content'] = 'aboutus';
		$this->load->view('loggedin_template', $data);
        }

        /**
         * Method for processing a user logout.
         */
        function logout()
	{
                //Kill the session
                $this->session->sess_destroy();

                //Go home
                redirect(base_url());
        }
}
?>
